<template>
  <div :class="{fullscreen:fullscreen}" :style="{ width: containerWidth }" class="tinymce-container">
    <textarea :id="tinymceId" class="tinymce-textarea" />
  </div>
</template>
<script>
import loadScript from './dynamicLoadScript'
import toolbar from './toolbar'
import plugins from './plugins'

const tinymceCDN = 'https://cdn.jsdelivr.net/npm/tinymce@5.8.2/tinymce.min.js'
const tinymceZHCNCDN = 'https://cdn.jsdelivr.net/npm/tinymce-i18n@20.12.25/langs/zh_CN.js'
const languageTypes = {
  en: 'en',
  zh: 'zh_CN',
  es: 'es_MX',
  ja: 'ja'
}
export default {
  name: 'tinymceText',
  props: {
    // v-model
    value: {
      type: String,
      default: ''
    },
    id: {
      type: [Number, String],
      default: () => {
        return 'vue-tinymce'
      }
    },
    // 工具bar配置
    toolbar: {
      type: Array,
      default: () => []
    },
    // 菜单配置
    menubar: {
      type: String,
      default: 'file edit insert view format table'
    },
    height: {
      type: [Number, String],
      default: 'auto'
    },
    width: {
      type: [Number, String],
      default: 'auto'
    }
  },
  data() {
    return {
      tinymceId: this.id,
      hasChange: false,
      hasInit: false,
      fullscreen: false
    }
  },
  computed: {
    // tinymce容器宽度
    containerWidth() {
      const width = this.width
      // 匹配`100`, `'100'`
      const regWidth = /^[\d]+(\.[\d]+)?$/
      if (regWidth.test(width)) {
        return width + 'px'
      }
      return width
    },
    language() {
      return languageTypes[this.$store.getters['language/language']] || 'zh_CN'
    }
  },
  watch: {
    value(val) {
      if (!this.hasChange && this.hasInit) {
        this.$nextTick(() => {
          window.tinymce.get(this.tinymceId).setContent(val || '')
        })
      }
    },
    language() {
      this.destroyTinymce()
      this.$nextTick(() => this.optTinymce())
    }
  },
  mounted() {
    this.initTinymce()
  },
  destroyed() {
    this.destroyTinymce()
  },
  methods: {
    initTinymce() {
      // 加载tinymce cdn链接
      loadScript(tinymceCDN, (err) => {
        if (err) {
          return this.$message.error(err.message)
        }
        loadScript(tinymceZHCNCDN)
        this.optTinymce()
      })
    },
    optTinymce() {
      const _this = this
      window.tinymce.init({
        selector: `#${this.tinymceId}`,
        language: this.language,
        height: this.height,
        body_class: 'panel-body ',
        object_resizing: false,
        toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
        menubar: this.menubar,
        plugins: plugins,
        end_container_on_empty_block: true,
        powerpaste_word_import: 'clean',
        code_dialog_height: 450,
        code_dialog_width: 1000,
        advlist_bullet_styles: 'square',
        advlist_number_styles: 'default',
        default_link_target: '_blank',
        link_title: false,
        nonbreaking_force_tab: true,
        init_instance_callback: editor => {
          _this.value && editor.setContent(_this.value)

          _this.hasInit = true
          editor.on('NodeChange Change KeyUp SetContent', () => {
            this.hasChange = true
            this.$emit('input', editor.getContent())
          })
        },
        setup: editor => {
          editor.on('FullScreenStateChanged', (event) => {
            _this.fullscreen = event.state
          })
        },
        convert_urls: false
      })
    },
    setContent(value) {
      window.tinymce.get(this.tinymceId).setContent(value)
    },
    getContent() {
      window.tinymce.get(this.tinymceId).getContext()
    },
    destroyTinymce() {
      const tinymce = window.tinymce.get(this.tinymceId)

      this.fullscreen && tinymce.execCommand('mceFullScreen')

      tinymce && tinymce.destroy()
    }
  }
}
</script>
<style lang="scss">
</style>
